package cst.a722wr.zyfypt_722wr.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import cst.a722wr.zyfypt_722wr.R;
import cst.a722wr.zyfypt_722wr.iface.wrListener;
import cst.a722wr.zyfypt_722wr.model.ResModel;

public class RegActivity extends AppCompatActivity {

    private EditText reg_username;
    private EditText reg_password;
    private EditText reg_password_ok;
    private EditText reg_tel;
    private EditText reg_mail;
    private EditText reg_realname;
    private Button reg;
    private Button regReturn;
    private RadioButton reg_student;
    private ResModel model;
    private wrListener<String> listener;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        reg_username = (EditText) findViewById(R.id.reg_username);
        reg_password = (EditText) findViewById(R.id.reg_password);
        reg_password_ok = (EditText) findViewById(R.id.reg_password_ok);
        reg_tel = (EditText) findViewById(R.id.reg_tel);
        reg_mail = (EditText) findViewById(R.id.reg_mail);
        reg_realname = (EditText) findViewById(R.id.reg_realname);
        reg_student = (RadioButton) findViewById(R.id.reg_student);

        listener = new wrListener<String>() {
            @Override
            public void onResponse(String s) {
                if(s.equals("1")){
                    Toast.makeText(RegActivity.this, "注册成功，返回登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(RegActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else
                    Toast.makeText(RegActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(RegActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };

        reg = (Button) findViewById(R.id.reg_ok);
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!reg_password.getText().toString().isEmpty())
                {
                    if(reg_password.getText().toString().equals(reg_password_ok.getText().toString())){
                        model = new ResModel("scalar");
                        if(reg_student.isChecked())
                            model.reg(reg_username.getText().toString(), reg_password.getText().toString(),
                                    reg_tel.getText().toString(), 2, reg_mail.getText().toString(), reg_realname.getText().toString(), listener);
                        else
                            model.reg(reg_username.getText().toString(), reg_password.getText().toString(),
                                    reg_tel.getText().toString(), 3, reg_mail.getText().toString(), reg_realname.getText().toString(), listener);
                    }
                    else{
                        Toast.makeText(RegActivity.this, "前后密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(RegActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        regReturn = (Button) findViewById(R.id.reg_return);
        regReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(RegActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
